<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="访客名称" prop="driverName" >
        <el-input
          style="width: 240px"
          v-model="queryParams.driverName"
          placeholder="请输入访客名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          style="width: 240px"
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          style="width: 240px"
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="licensePlate">
        <el-input
          style="width: 240px"
          v-model="queryParams.licensePlate"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="访问日期">
        <el-date-picker
          clearable
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vqms:visitRecord:yuchai:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['vqms:visitRecord:yuchai:delete']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['vqms:visitRecord:yuchai:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="visitRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属工厂" align="center" prop="factoryName" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="访客名称" align="center" prop="driverName" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="性别" align="center" prop="sex" :show-overflow-tooltip="true" width="100">
        <template v-slot="scope">
          <span v-if="scope.row.sex === 0">女</span>
          <span v-if="scope.row.sex === 1">男</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phone" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="身份证号" align="center" prop="idCard" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="车牌号" align="center" prop="licensePlate" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="本人单位" align="center" prop="workCompany" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="到访单位" align="center" prop="visitCompany" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="是否携带电脑" align="center" prop="isComputer" width="150">
        <template v-slot="scope">
          <span v-if="scope.row.isComputer === 0" style="color: red;">否</span>
          <span v-if="scope.row.isComputer === 1" style="color: green;">是</span>
        </template>
      </el-table-column>
      <el-table-column label="来访者服" align="center" prop="clothing" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="对接人" align="center" prop="visitName" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="来访事由" align="center" prop="visitReason" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="访问时间" align="center" prop="createTime" :show-overflow-tooltip="true"  width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right"  width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['vqms:visitRecord:yuchai:update']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['vqms:visitRecord:yuchai:delete']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改访客记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12" >
            <el-form-item label="所属工厂" prop="factoryName">
              <el-autocomplete
                clearable
                style="width:100%;"
                v-model="form.factoryName"
                value-key="name"
                :fetch-suggestions="querySearchAsync"
                placeholder="请输入所属工厂"
                @select="handleSelectFactory">
              </el-autocomplete>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="访客名称" prop="driverName">
              <el-input v-model="form.driverName" placeholder="请输入访客名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="idCard">
              <el-input v-model="form.idCard" placeholder="请输入身份证号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="车牌号" prop="licensePlate">
              <el-input v-model="form.licensePlate" placeholder="请输入车牌号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option v-for="sex in sexList" :key="sex.value" :label="sex.label" :value="sex.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="来访者服" prop="clothing">
              <el-input v-model="form.clothing" placeholder="请输入来访者服" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否携带电脑" prop="isComputer">
              <el-select v-model="form.isComputer" placeholder="请选择是否携带电脑">
                <el-option v-for="isComputer in isComputerList" :key="isComputer.value" :label="isComputer.label" :value="isComputer.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="本人单位" prop="workCompany">
              <el-input v-model="form.workCompany" placeholder="请输入本人单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="到访单位" prop="visitCompany">
              <el-input v-model="form.visitCompany" placeholder="请输入到访单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="对接人" prop="visitName">
              <el-input v-model="form.visitName" placeholder="请输入对接人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="来访事由" prop="visitReason">
              <el-input type="textarea" v-model="form.visitReason" placeholder="请输入来访事由" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { selectFactoryList } from "@/api/vqms/factory";
import { listVisitRecordYuchai,getVisitRecordYuchai,addVisitRecordYuchai,updateVisitRecordYuchai,delVisitRecordYuchai } from "@/api/vqms/visitRecordYuchai";

export default {
  //name: "visitRecordYuchai",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 访客表格数据
      visitRecordList: [],
      // 性别
      sexList :[
        {
          label:"女",
          value:0
        },
        {
          label:"男",
          value:1
        }
      ],
      // 是否携带电脑
      isComputerList :[
        {
          label:"否",
          value:0
        },
        {
          label:"是",
          value:1
        }
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createBy: undefined,
        status: undefined
      },
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        factoryName: [
          { required: true, message: "所属工厂不能为空", trigger: "change" }
        ],
        driverName: [
          { required: true, message: "访客名称不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          { pattern: /^(([1-9]{1}\d*)|(0{1}))?$/,message: "只能输入整数",trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询访客列表 */
    getList() {
      this.loading = true;
      listVisitRecordYuchai(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.visitRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.$set(this.form, "sex", 1);
      this.$set(this.form, "isComputer", 0);
      this.open = true;
      this.title = "添加访客记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getVisitRecordYuchai(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改访客记录";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateVisitRecordYuchai(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVisitRecordYuchai(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids
      this.$modal.confirm('是否确认删除？').then(function() {
        return delVisitRecordYuchai(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vqms/visitRecord/yuchai/export', {
        ...this.queryParams
      }, `访客记录.xlsx`)
    },
    //所属工厂异步搜索
    async querySearchAsync(queryString, cb) {
      this.factoryList=[];
      let query={
        name : queryString
      }
      await selectFactoryList(query).then(response => {
        this.factoryList = response.data
      });
      var results = queryString ? this.factoryList.filter(this.createStateFilter(queryString)) : this.factoryList;
      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        cb(results);
      }, 1000 * Math.random());
    },
    createStateFilter(queryString) {
      return (state) => {
        return (state.name.indexOf(queryString) >= 0);
      };
    },
    handleSelectFactory(item) {
      this.form.factoryName=item.name
      this.form.factoryId=item.id
    },
  }
};
</script>
